<?php
/**
 * User: PunkVv
 * Date: 2016/6/16
 * Time: 15:57
 */
namespace app\common\behavior;

use think\Db;
use think\Hook;

/**
 * 初始化钩子信息
 * Class InitHookBehavior
 * @package app\common\behavior
 */
class InitHookBehavior
{
    public function run(&$params)
    {
        //添加插件配置
//        $addon_config['ADDON_PATH'] = './addons/';
//        $addon_config['AUTOLOAD_NAMESPACE'] = config('AUTOLOAD_NAMESPACE');
//        $addon_config['AUTOLOAD_NAMESPACE']['addons'] = $addon_config['ADDON_PATH'];
//        config($addon_config);
        $data = cache('hooks');
        if (!$data) {
            $hooks = db('hook')->column('name,addons');
            foreach ($hooks as $key => $value) {
                if ($value) {
                    $map['status'] = 1;
                    $names = explode(',', $value);
                    $map['name'] = array('IN', $names);
                    $data = db('addon')->where($map)->column('id,name');
                    if ($data) {
                        $addons = array_intersect($names, $data);
                        Hook::add($key, array_map('get_addon_class', $addons));
                    }
                }
            }
            cache('hooks', Hook::get());
        } else {
            Hook::import($data, false);
        }
    }
}